package com.bonc.dao;

import com.bonc.bean.User;
import com.bonc.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    public List<User> getAll() {
        List<User> userList = new ArrayList<User>();

        String sql = "select * from tbl_users";

        ResultSet result = null;
        PreparedStatement pre = null;
        DBUtils dbUtils = new DBUtils();
        Connection conn = dbUtils.getConn();
        try {
            pre = conn.prepareStatement(sql);
            result = pre.executeQuery();

            while (result.next()) {
                User user = new User();
                user.setNumber(result.getString("number"));
                user.setUserName(result.getString("name"));
                user.setMoblie(result.getString("mobile"));
                user.setTel(result.getString("tel"));
                user.setEmail(result.getString("email"));
                userList.add(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (result != null)
                    result.close();
                if (pre != null)
                    pre.close();
                if (conn != null)
                    conn.close();
                System.out.println("数据库连接已关闭！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return userList;
    }

}
